package com.k.base.jdk.stream.listquchongfu.quchong1;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;

public class PersonOneTest {
    public static void main(String[] args) {
        List<PersonOne> list = new ArrayList<>();
        PersonOne p1 = new PersonOne("1", "137", "zs", 25);
        PersonOne p2 = new PersonOne("1", "137", "ls", 25);
        PersonOne p3 = new PersonOne("1", "137", "ww", 25);
        PersonOne p4 = new PersonOne("1", "137", "ww", 25);
        PersonOne p5 = new PersonOne("1", "137", "ls", 25);

        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);
        list.add(p5);
        System.out.println(list);

        List<PersonOne> listResultV1 = v1(list);
        System.out.println(listResultV1);

        List<PersonOne> listResultV2 = v2(list);
        System.out.println(listResultV2);
    }

    // 去重方式一
    private static List<PersonOne> v1(List<PersonOne> list) {
        return new ArrayList<>(new LinkedHashSet<>(list));
    }

    // 去重方式二
    private static List<PersonOne> v2(List<PersonOne> list) {
        return list.stream().distinct().collect(Collectors.toList());
    }

    // 去重方式三
    private static List<PersonOne> v3(List<PersonOne> list){
        List<PersonOne> newList = new ArrayList<>();
        for(PersonOne personOne:list){
            String groupId = personOne.getGroupId();
            String number = personOne.getNumber();
            String name = personOne.getName();
            Integer age = personOne.getAge();
            if(newList.size()<1){
                newList.add(personOne);
            }
            if(!existInNewList(groupId,number,name,age,newList)){
                newList.add(personOne);
            }
        }
        return newList;
    }

    private static boolean existInNewList(String groupId, String number, String name, Integer age, List<PersonOne> newList) {
        return newList.stream()
                .anyMatch(a->a.getGroupId().equals(groupId) && a.getNumber().equals(number) && a.getName().equals(name) && a.getAge().equals(age));
    }
}
